//package com.lagou.boot;
//
//import com.lagou.client.RPCConsumer;
//import com.lagou.service.IUserService;
//import com.lagou.zookeeper.GetNodeData;
//import com.lagou.zookeeper.ZkConstant;
//import org.apache.zookeeper.KeeperException;
//
//import java.io.IOException;
//import java.io.UnsupportedEncodingException;
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//import java.util.Random;
//
//public class LoadBalanceConsumerBoot {
//
//    //参数定义
//    private static final String PROVIDER_NAME = "UserService#sayHello#";
//
//    public static void main(String[] args) throws InterruptedException, IOException, KeeperException {
////        //初始化zk
//        GetNodeData.init();
//        Thread.sleep(2000);
//        //1.创建代理对象
//        RPCConsumer rpcConsumer = new RPCConsumer();
//        IUserService service = (IUserService) rpcConsumer.createProxy(IUserService.class, PROVIDER_NAME);
//        //2.循环给服务器写数据
//        while (true) {
//            //负载均衡
//            String address = loadBalance();
//            String[] arr = address.split(":");
//            //初始化客户端
//            rpcConsumer.initClient(arr[0], Integer.valueOf(arr[1]));
//            String result = service.sayHello("are you ok !!");
//            System.out.println(result);
//            Thread.sleep(2000);
//        }
//    }
//
//    private static List<String> getChildren() throws KeeperException, InterruptedException {
//        List<String> children = GetNodeData.zooKeeper.getChildren(ZkConstant.ROOT_NODE, true);
//        return children;
//    }
//
//
//    private static String loadBalance() throws KeeperException, InterruptedException, UnsupportedEncodingException {
//        List<String> children = getChildren();
//        Map<String, Long> dataMap = getData(children);
//        int minIndex = new Random().nextInt(children.size());
//        //负载均衡
//        for (int i = 1; i < children.size(); i++) {
//            if (children.size() == 1) {
//                minIndex = i;
//                return children.get(minIndex);
//            } else {
//                if (dataMap.get(children.get(i - 1)) < dataMap.get(children.get(i))) {
//                    minIndex = i - 1;
//                } else if (dataMap.get(children.get(i - 1)) > dataMap.get(children.get(i))) {
//                    minIndex = i;
//                } else {
//                    return children.get(minIndex);
//                }
//            }
//        }
//        return children.get(minIndex);
//    }
//
//    private static Map<String, Long> getData(List<String> children) throws UnsupportedEncodingException, KeeperException, InterruptedException {
//        Map<String, Long> dataMap = new HashMap<>();
//        for (String child : children) {
//            byte[] bytes = GetNodeData.zooKeeper.getData(ZkConstant.ROOT_NODE + "/" + child, true, null);
//            System.out.println(ZkConstant.ROOT_NODE + "/" + child + "节点数据=====" + new String(bytes, "UTF-8"));
//            dataMap.put(child, Long.valueOf(new String(bytes, "UTF-8")));
//        }
//        return dataMap;
//    }
//}